<style>
.time-tap{
  overflow: hidden;
}
.time-tap>div{
  margin-bottom:25px;
}
.time-tap>div>span{
  display: inline-block;
  width: 150px;
}
</style>
<template>
  <div>
    <div class="main-table">
      <el-tabs v-model="addNpcTask" @tab-click="handleClick">
        <el-tab-pane label="基本信息" name="first">
          <bidMain :npcTaskForm="npcTaskForm" ref="npcTask" @radioChange="getRadioVal"></bidMain>
        </el-tab-pane>
        <el-tab-pane label="招标标的" name="second">
          <bidMaterials :tableData="materialsJsonStr" :purOrgId="purOrgId" :factoryId="factoryId" @radioChange="getRadioVal"></bidMaterials>
        </el-tab-pane>
        <el-tab-pane label="选择供应商" name="thirdly">
          <bidVendors ref="trans"
            @transAdd="addTrans" width="100%" :checkName="['vendorCode','vendorName']"
            :title="{left:'可选供应商',right:'已选供应商'}" :isQsparams="false"
            :searchForm="searchForm" :selectData="vendorsJsonStr" @radioChange="getRadioVal"
            :url="lib.config.host.system + '/system/orgmgt/organization/queryForPsDatalist'">
          </bidVendors>
        </el-tab-pane>
      </el-tabs>
    </div>
    <div style="margin-left:30px" class="el_submit" >
      <el-button type="primary" @click="saveOrUpdate()">保存</el-button>
      <el-button @click="$router.push({name:'bidmgt'})">返回</el-button>
    </div>
  </div>
</template>
<script>
  import qs from 'qs';
  import bidMain from './bidMain.vue'
  import bidMaterials from './bidMaterials.vue'
  import bidVendors from './bidVendors.vue'
  export default {
    components: {
      bidMain,
      bidMaterials,
      bidVendors
    },
    data() {
      return {
        addNpcTask: 'first',
        npcTaskForm:{
          tenderEtimeStr:'',
          openTimeStr:'',
          tenderEtimeStr2:'01',
          openTimeStr2:'01'
        },
        dataVendorList:[],
        materialsJsonStr:[],
        vendorsJsonStr :[],
        purOrgId:'',
        factoryId:'',
        searchForm:[
          {key:'vendorSource',type:'',value:[]},
          {key:'vendorName',type:'text',placeholder:'请输入名称',value:''},
				  {key:'sourceType',type:'radio',placeholder:'',value:2,child:[{label:1,value:'按品类'},{label:2,value:'按货源'}]}
        ]
      };
    },
    created(){
      if(this.$route.query.id){
        this.axios.post(this.lib.config.host.bidtender + `/bidmgt/view/editBid/${this.$route.query.id}`).then((res) => {
          if(res.data.success){
            this.npcTaskForm={...res.data.resultMap.bidMainDto,tenderEtimeStr2:'',openTimeStr2:''};
            this.materialsJsonStr=res.data.resultMap.bidMaterials;
            this.vendorsJsonStr=res.data.resultMap.bidVendors;
            this.npcTaskForm.tenderEtimeStr2=!!this.npcTaskForm.tenderEtimeStr?this.npcTaskForm.tenderEtimeStr.slice(11,13):'';
            this.npcTaskForm.openTimeStr2=!!this.npcTaskForm.openTimeStr?this.npcTaskForm.openTimeStr.slice(11,13):'';
            this.npcTaskForm.tenderEtimeStr=!!this.npcTaskForm.tenderEtimeStr?this.npcTaskForm.tenderEtimeStr.split(' ')[0]:'';
            this.npcTaskForm.openTimeStr=!!this.npcTaskForm.openTimeStr?this.npcTaskForm.openTimeStr.split(' ')[0]:'';
            this.$refs.npcTask.handleChange(this.npcTaskForm.purOrgId);
            this.$nextTick(()=>{
              this.$refs.trans.resetCheck();
            })
          }
        })
        .catch(function (error) {
            console.log(error);
        });
      }
      // Add By masn 2017/6/7
      if(this.$route.query.routerSource === 'prRequest'){ // 开发申请转询价
        if(this.$route.query.materialData.length > 0){
          this.$route.query.materialData.forEach(element =>{
            let tableMaterial ={
              'purOrgId' : element.purOrgId,
              'purOrgCode' : element.purOrgCode,
              'purOrgName' : element.purOrgName,
              'materialId' : element.materialId,
              'materialCode' : element.materialCode,
              'materialName' : element.materialName,
              'factoryId' : element.factoryId,
              'factoryName' : element.factoryName,
              'stockUnit' : element.purUnitName,
              'planQty' : element.purchaseQty,
              'remarks' : element.remarks,
            }
            this.materialsJsonStr.push(tableMaterial);
          })
        }
        this.vendorsJsonStr =  this.$route.query.vendorData;
        console.log(this.$route.query.materialData);
        console.log(this.$route.query.vendorData);
        // 赋值给供应商穿梭框
        this.searchTrans();
      }
      // Add By masn 2017/6/7
    },
    methods: {
      handleClick(tab, event) {
        switch (this.addNpcTask){
          case 'first':
            break;
          case 'second':
            break;
          case 'thirdly':
            //选择供应商后查询右侧穿梭框数据
            this.$refs.trans.initData();
            break;
          default:
            break;
        }
      },
      // 这个方法有没有用到？
      // searchTrans(parameter){
      //   //穿梭框右边数据获取
      //   var par = {};
      //   if (parameter != "") {
      //     par = parameter
      //   }
      //   this.axios.post(this.lib.config.host.system + "/system/orgmgt/organization/queryForNpcDatalist", qs.stringify(par)).then((res) => {
      //     this.selectData=res.data.rows;
      //   })
      //   .catch(function (error) {
      //       console.log(error);
      //   });
      // },
      addTrans(data){
      },
      // Add By masn 2017/6/7
      searchTrans(){
        this.vendorsJsonStr.forEach(element=>{
          element.id = element.vendorId;
        })
        this.$nextTick(()=>{
          this.$refs.trans.searchTrans({pageNo:1, pageSize:10});
        })
      },
      // Add By masn 2017/6/7
      getRadioVal(val){
        this.$refs.trans.searchFormat.vendorSource=[];
        if(val==1){
          this.$refs.trans.searchFormat.vendorSource=this.npcTaskForm.categoryId?[this.npcTaskForm.categoryId]:[];
        }else{
          this.materialsJsonStr.forEach(item=>{
            if(!!item.materialId){
              this.$refs.trans.searchFormat.vendorSource.push(item.materialId)
            }
          })
          //this.$refs.trans.searchFormat.vendorSource=this.$refs.trans.searchFormat.vendorSource.join(',')
        }
      },
      // 保存|更新
      saveOrUpdate(){
        let check=false;
        this.$refs.npcTask.$refs.bidInfo.validate((valid)=>{
          check=valid
        })
        if(!check){
          this.addNpcTask='first';
          return
        }
        if(!this.materialsJsonStr.length){
          this.addNpcTask='second';
          this.$message.info('请添加招标标的！');
          return
        }
        if(!this.vendorsJsonStr.length){
          this.addNpcTask='thirdly';
          this.$message.info('请添加供应商！');
          return
        }
        if(this.vendorsJsonStr.length<this.npcTaskForm.minOpenNum){
          this.addNpcTask='thirdly';
          this.$message.info('供应商数量必须大于等于最小开标数量！');
          return
        }
        let vendorsJsonStr=this.vendorsJsonStr.map(item=>{
          return{
            id:item.id,
            vendorCode:item.vendorCode,
            vendorName:item.vendorName,
          }
        })
        this.materialsJsonStr.forEach(element =>{
            delete element.showTextarea;
            delete element.factoryIds;
        })
        let data={
          ...this.npcTaskForm,
          materialsJsonStr:JSON.stringify(this.materialsJsonStr),
          vendorsJsonStr:JSON.stringify(vendorsJsonStr)
        }
        data.tenderEtimeStr=data.tenderEtimeStr+"T"+data.tenderEtimeStr2+":00:00";
        data.openTimeStr=data.openTimeStr+"T"+data.openTimeStr2+":00:00";
        console.log(data)
        this.axios.post(this.lib.config.host.bidtender + "/bidmgt/bid/saveOrUpdate",data).then((res) => {
           // 保存后跳转到主页面
           if(res.data.success){
            this.$message('添加成功！');
            this.$router.push({'name':'bidmgt'});
           }
        })
        .catch(function (error) {
            console.log(error);
        });
      }
    }
  };
</script>
